package com.operator.Activitys.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.operator.Activitys.R;
import com.operator.Activitys.activity.InstallActivity;
import com.operator.Activitys.activity.PrettySignActivity;
import com.operator.Activitys.activity.SaleActivity;
import com.operator.Activitys.activity.SelfQueryActivity;
import com.operator.Activitys.activity.SupplyActivity;
import com.operator.Activitys.activity.WebDateActivity;
import com.operator.Activitys.adapter.HomeMenuAdapter;
import com.operator.Activitys.adapter.holder.BannerViewHolder;
import com.operator.Activitys.adapter.transformer.AlphaPageTransformer;
import com.operator.Activitys.bean.HomeMenu;
import com.operator.Activitys.bean.ImageText;
import com.operator.Activitys.fragment.base.BaseFragment;
import com.operator.Activitys.http.manager.HomeManager;
import com.operator.Activitys.utils.Constant;
import com.operator.Activitys.utils.FrescoUtils;
import com.operator.Activitys.utils.JumperUtils;
import com.operator.Activitys.utils.LogUtils;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 项目名称：金泽云 手机app
 * 类描述：首页
 * 创建人：张飞祥
 * 创建时间：2018/4/27 14:29
 * 修改人：张飞祥
 * 修改时间：2018/4/27 14:29
 * 修改备注：
 */
public class HomeFragment extends BaseFragment {

    //标题
    @Bind(R.id.tv_title)
    TextView tvTitle;
    //轮播图
    @Bind(R.id.mv_home_banner)
    MZBannerView mvHomeBanner;
    //图文资讯
    @Bind(R.id.sv_home_info)
    SimpleDraweeView svHomeInfo;
    //号卡销售
    @Bind(R.id.iv_home_sale)
    ImageView ivHomeSale;
    //宽带安装
    @Bind(R.id.iv_home_install)
    ImageView ivHomeInstall;
    //待定
    @Bind(R.id.iv_home_uncertain)
    ImageView ivHomeUncertain;
    //自助查询
    @Bind(R.id.iv_home_query)
    ImageView ivHomeQuery;
    //通讯供求
    @Bind(R.id.iv_home_supply)
    ImageView ivHomeSupply;
    //功能选择菜单
    @Bind(R.id.vp_home_menu)
    ViewPager vpHomeMenu;

    //功能栏 数据适配器
    private HomeMenuAdapter mMenuAdapter = null;

    //初始化网络请求
    public HomeManager mApiManager;

    @Override
    protected void initViews() {
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getHomeApiManager(getActivity(),getActivity().getTaskId());
        //设置标题
        tvTitle.setText("首页");
        //TODO 网络请求加载轮播图
        mApiManager.homeBanner();
        //TODO 网络请求加载广告
        mApiManager.homeAdInfo();
        //TODO 网络请求加载功能栏
        mApiManager.homeMenu();
    }


    /**
     * 加载 轮播图 返回结果
     */
    @Subscriber(tag = Constant.BUS_HOME_BANNER)
    public void onBanner(List<ImageText> list) {
        //加载轮播图
        initBannerImages(list);
    }

    /**
     * 加载 广告 返回结果
     */
    @Subscriber(tag = Constant.BUS_HOME_ADINFO)
    public void onAdInfo(final ImageText adInfo) {
        //加载图片
        FrescoUtils.loadImage(adInfo.getImgUrl(),svHomeInfo);
        //广告 点击事件
        svHomeInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                //id
                bundle.putInt(Constant.WEB_ID,adInfo.getId());
                //标题
                bundle.putString(Constant.WEB_TITLE,"");
                //地址
                bundle.putString(Constant.WEB_URL,adInfo.getActionUrl());
                //开始跳转
                JumperUtils.JumpTo(getActivity(), WebDateActivity.class,bundle);
            }
        });
    }

    /**
     * 加载 轮播图 返回结果
     */
    @Subscriber(tag = Constant.BUS_HOME_MENU)
    public void onMenu(List<HomeMenu> list) {
        //加载轮播图
        initMenuImages(list);
    }

    /**
     * 初始化加载轮播图
     */
    private void initBannerImages(final List<ImageText> imgs) {
        //去掉 圆点 指示器
        mvHomeBanner.setIndicatorVisible(false);

        //数据重装
        List<String> list = new ArrayList<>();
        for (int i = 0; i < imgs.size(); i++) {
            list.add(imgs.get(i).getImgUrl());
        }
        // 设置数据源
        mvHomeBanner.setPages(list, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });

        //TODO 轮播图 点击事件
        mvHomeBanner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {

                Bundle bundle = new Bundle();
                //id
                bundle.putInt(Constant.WEB_ID,imgs.get(position).getId());
                //标题
                bundle.putString(Constant.WEB_TITLE,"");
                //地址
                bundle.putString(Constant.WEB_URL,imgs.get(position).getActionUrl());
                //开始跳转
                JumperUtils.JumpTo(getActivity(), WebDateActivity.class,bundle);
            }
        });

        //开始轮播图
        mvHomeBanner.start();

    }

    /**
     * 初始化 功能选择菜单
     */
    private void initMenuImages(final List<HomeMenu> list) {
        //填充 adapter 数据
        mMenuAdapter = new HomeMenuAdapter(getActivity(),list) {
            @Override
            public void setItemOnClickListener(int position) {

                //获取第 i 个 数据实体
                HomeMenu menu = list.get(position);

                //TODO  传递 方法
                Bundle bundle = new Bundle();
                //标题
                bundle.putString(Constant.WEB_TITLE,menu.getTitle());
                //地址
                bundle.putString(Constant.WEB_URL,menu.getActionUrl());
                //开始跳转
                JumperUtils.JumpTo(getActivity(), WebDateActivity.class,bundle);
            }
        };
        //设置 viewPager 默认显示几页
        vpHomeMenu.setOffscreenPageLimit(3);
        //设置 viewPager 动画效果
        vpHomeMenu.setPageTransformer(true,new AlphaPageTransformer());
        //设置 viewPager 数据适配器
        vpHomeMenu.setAdapter(mMenuAdapter);
        //设置 viewPager 默认显示的页码
        vpHomeMenu.setCurrentItem(1);
    }


    @Override
    public void onPause() {
        super.onPause();
        //停止轮播图
        mvHomeBanner.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        //开始轮播图
        mvHomeBanner.start();
    }

    /**
     * 按钮点击事件
     * @return
     */
    @OnClick({R.id.iv_home_sale,R.id.iv_home_install,R.id.iv_home_uncertain,
            R.id.iv_home_query,R.id.iv_home_supply})
    public void onClick(View view){
        switch (view.getId()){

            case R.id.iv_home_sale:         //号卡销售
                JumperUtils.JumpTo(getActivity(), SaleActivity.class);
                break;
            case R.id.iv_home_install:      //宽带安装
                JumperUtils.JumpTo(getActivity(), InstallActivity.class);
                break;

            case R.id.iv_home_uncertain:    //待定
                JumperUtils.JumpTo(getActivity(), PrettySignActivity.class);
                break;

            case R.id.iv_home_query:        //自助查询
                JumperUtils.JumpTo(getActivity(), SelfQueryActivity.class);
                break;

            case R.id.iv_home_supply:       //通讯供求
                JumperUtils.JumpTo(getActivity(), SupplyActivity.class);
                break;
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.frg_home;
    }

}
